let rate = 0.9;

function request() {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
			let res = Math.random();
			if (res > rate) {
				resolve(res)
			} else {
				reject(res);
			}
		})
	})
}


function retry(func, times = 0, delay = 0) {
	return new Promise((resolve, reject) => {
		const inner = async function inner() {
			try {
				const result = await func();
				resolve(result)
			} catch (e) {
				if (times-- <= 0) {
					reject(e);
				} else {
					console.log('开始重试,剩余：', times)
					setTimeout(() => {
						inner();
					}, delay)
				}
			}
		}
		inner();
	})
}


retry(request, 3, 2000)
.then(res => {
	console.log(res, 'success')
})
.catch(e => {
	console.log(e, 'error')
})